package com.lagou.edu.ad.remote;

import com.lagou.edu.ad.api.dto.PromotionAdDTO;
import com.lagou.edu.ad.api.dto.PromotionSpaceDTO;
import com.lagou.edu.ad.entity.PromotionAd;
import com.lagou.edu.ad.entity.PromotionSpace;
import com.lagou.edu.ad.service.IPromotionAdService;
import com.lagou.edu.ad.service.IPromotionSpaceService;
import com.lagou.edu.common.util.ConverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * TODO
 *
 * @author lm
 * @since 2021/10/13 14:56
 */
@Service
public class AdService {
    @Autowired
    IPromotionSpaceService promotionSpaceService;

    @Autowired
    IPromotionAdService promotionAdService;

    public List<PromotionSpaceDTO> getAllSpaces() {
        List<PromotionSpace> promotionSpaceList = promotionSpaceService.list();
        return ConverUtil.convertList(promotionSpaceList, PromotionSpaceDTO.class);
    }

    public List<PromotionSpaceDTO> getSpaceAndAd(String[] spaceKeys) {
        List<PromotionSpaceDTO> promotionSpaceDTOList = new ArrayList<>();

        for (String spaceKey : spaceKeys){
            //根据spaceKey获取PromotionSpace
            PromotionSpace promotionSpace = promotionSpaceService.getPromotionSpaceBySpaceKey(spaceKey);
            if(promotionSpace == null){
                continue;
            }
            //根据PromotionSpaceId获取对应的PromotionAd
            List<PromotionAd> promotionAds = promotionAdService.getPromotionAdBySpaceId(promotionSpace.getId());
            PromotionSpaceDTO promotionSpaceDTO = new PromotionSpaceDTO();
            List<PromotionAdDTO> promotionAdDTOS = new ArrayList<>(promotionAds.size());

            //拷贝promoteSpace对象的属性到promoteSpaceDTO
            ConverUtil.convert(promotionSpace, promotionSpaceDTO);

            for (PromotionAd promotionAd : promotionAds) {
                PromotionAdDTO promotionAdDTO = new PromotionAdDTO();
                ConverUtil.convert(promotionAd, promotionAdDTO);
                promotionAdDTOS.add(promotionAdDTO);
            }

            promotionSpaceDTO.setAdDTOList(promotionAdDTOS);
            promotionSpaceDTOList.add(promotionSpaceDTO);
        }
        return promotionSpaceDTOList;
    }

    public PromotionSpaceDTO getSpaceById(Integer id) {
        PromotionSpace promotionSpace = promotionSpaceService.getById(id);
        return ConverUtil.convert(promotionSpace, PromotionSpaceDTO.class);
    }

    public Boolean saveOrUpdateSpace(PromotionSpaceDTO promotionSpaceDTO) {
        PromotionSpace promotionSpace = new PromotionSpace();
        ConverUtil.convert(promotionSpaceDTO, promotionSpace);
        Boolean result;
        //保存或者更新
        try {
            promotionSpace.setUpdateTime(new Date());
            if (promotionSpace.getId() != null) {
                result = promotionSpaceService.updateById(promotionSpace);
            } else {
                promotionSpace.setIsDel(0);
                promotionSpace.setCreateTime(new Date());
                result = promotionSpaceService.save(promotionSpace);
            }
        } catch (Exception e) {
            throw e;
        }
        return result;
    }

    public PromotionAdDTO getAdById(Integer id) {
        PromotionAd promotionAd = promotionAdService.getById(id);
        return ConverUtil.convert(promotionAd, PromotionAdDTO.class);
    }

    public List<PromotionAdDTO> getAdList() {
        List<PromotionAd> promotionAdList = promotionAdService.list();
        return ConverUtil.convertList(promotionAdList, PromotionAdDTO.class);
    }

    public Boolean saveOrUpdateAd(PromotionAdDTO promotionAdDTO) {
        PromotionAd promotionAd = new PromotionAd();
        ConverUtil.convert(promotionAdDTO, promotionAd);
        boolean result;
        //保存或者更新
        try {
            promotionAd.setUpdateTime(new Date());
            if (promotionAd.getId() != null) {
                result = promotionAdService.updateById(promotionAd);
            } else {
                promotionAd.setCreateTime(new Date());
                result = promotionAdService.save(promotionAd);
            }
        } catch (Exception e) {
            throw e;
        }
        return result;
    }

    public Boolean updateStatus(Integer id, Integer status) {
        boolean result = false;
        try {
            if (status == 0 || status == 1) {
                PromotionAd promotionAd = new PromotionAd();
                promotionAd.setId(id);
                promotionAd.setStatus(status);
                result = promotionAdService.updateById(promotionAd);
            }
        } catch (Exception e) {
            throw e;
        }
        return result;
    }
}
